package com.kye.express.business.invoice_online.detail;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatFragment;
import com.kye.express.databinding.FragmentWaybilllistBinding;
import com.kye.express.utils.ScreenUtils;
import com.kye.model.bean.invoice_online.GetInvoiceDetailResultBean;
import com.kye.model.fake.FakeData;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 运单列表
 */
public class WaybillListFragment extends BaseAppCompatFragment {
    private static final String ARG_DATA = "data";
    private FragmentWaybilllistBinding mBinding;

    protected MultiTypeAdapter mAdapter;
    protected Items mDatas = new Items();
    private List<GetInvoiceDetailResultBean.WaybillNoDataBean> mWaybillNoDataBeans;

    public static WaybillListFragment newInstance(List<GetInvoiceDetailResultBean.WaybillNoDataBean> waybillNoDataBeans) {
        WaybillListFragment fragment = new WaybillListFragment();
        ArrayList<GetInvoiceDetailResultBean.WaybillNoDataBean> waybillNoDataBeans1 = new ArrayList<>();
        waybillNoDataBeans1.addAll(waybillNoDataBeans);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARG_DATA, waybillNoDataBeans1);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_waybilllist, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding = FragmentWaybilllistBinding.bind(view);
        getActivity().setTitle(getString(R.string.invoice_contain_waybill));
        initView();
        initEvent();
    }

    private void initView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mWaybillNoDataBeans = (List<GetInvoiceDetailResultBean.WaybillNoDataBean>) bundle.getSerializable(ARG_DATA);
            mDatas.addAll(mWaybillNoDataBeans);
        }
        mAdapter = new MultiTypeAdapter();
        mAdapter.register(GetInvoiceDetailResultBean.WaybillNoDataBean.class, new InvoiceWaybillltemViewBinder(getActivity()));
        mBinding.recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.recyclerview.setHasFixedSize(true);
        mBinding.recyclerview.setNestedScrollingEnabled(false);
        RecyclerViewDivider.with(getActivity())
                .color(getResources().getColor(R.color.grey))
                .size(ScreenUtils.dp2px(getActivity(), 1))
                .hideLastDivider()
                .build()
                .addTo(mBinding.recyclerview);
        mBinding.recyclerview.setItemAnimator(new DefaultItemAnimator());
        mAdapter.setItems(mDatas);
        mBinding.recyclerview.setAdapter(mAdapter);
    }

    private void initEvent() {

    }
}
